const request = require('../../utils/https.js')
var userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHideToast: false,
    isHideToast1: false,
    isHideToast2: false,
    isHideToast3:false,
    navList: ['收入明细', '支出明细'],
    active: 0,
    countDownNum: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    userId = wx.getStorageSync('userId');
  },
  goShopping() {
    wx.switchTab({
      url: '/pages/list/list',
    })
  },
  goiDentity() {
    wx.navigateTo({
      url: '/pages/identity_new/identity_new?add=oneAdd'
    })
  },
  //提现说明
  onToast() {
    const that = this;
    that.setData({
      isHideToast: true
    })
  },
  //收益说明
  onToast1() {
    const that = this;
    that.setData({
      isHideToast2: true
    })
  },
  shutToast() {
    const that = this;
    that.setData({
      isHideToast: false,
      isHideToast1: false,
      isHideToast2: false,
      isHideToast3: false
    })
  },
  onClick(event) {
    const that = this;
    let navIndex = event.detail.index;
    let navName = event.detail.title;
    let navList = that.data.navList;
    that.getOrderList(navIndex)
    that.setData({
      active: navIndex
    })

  },
  getOrderList(navIndex) {
    const that = this;
    let userId = wx.getStorageSync('userId');
    request._get(getApp().Api_url + 'profit/inOrOut', { //收入明细列表接口
      userId: userId,
      type: navIndex + 1,
      pageNum: 1,
      pageSize: 100
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        var orderList = res.data.data;
        console.log('orderList==>', orderList)
        that.setData({
          orderList: orderList
        })
      } else {
        that.setData({
          orderList: []
        })
      }
    }, function () {})
  },
  goWithdrawal() {
    const that = this;
    let userId = wx.getStorageSync('userId');
    if (that.data.info.canPay >= 100) { //判断余额能够体现
      //判断没有没有提交过身份信息
      request._get(getApp().Api_url + 'user/hasCert', {
        userId: userId
      }, function (res) {
        if (res.data.errMsg == 'OK') {
          console.log('检测用户是否 认证==>', res.data.data)
          if (res.data.data.has != 1) {
            wx.navigateTo({
              url: '/pages/withdrawal/withdrawal'
            })
          } else {
            wx.navigateTo({
              url: '/pages/identity_new/identity_new?add=oneAdd'
            })
          }

        }
      }, function () {})



    } else {
      wx.showToast({
        title: '提现金额不满100元,提现失败',
        icon: 'none',
        duration: 2000
      })

    }

  },
  goWithdrawal_1() {
    const that = this;
    wx.navigateTo({
      url: '/pages/withdrawal_1/withdrawal_1?states=' + that.data.states + '&cashId=' + that.data.cashId + '&directly=directly'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    //我的收益
    request._get(getApp().Api_url + 'profit/entry', {
      userId: userId
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        console.log('我的收益==>', res.data.data)
        that.setData({
          info: res.data.data,
          cashId: res.data.data.cashId, //提现订单ID
          can: res.data.data.can, //1, 没有提现订单, 0, 有提现订单
          withdraw: res.data.data.withdraw, //0,不允许,1,允许提现
          read: res.data.data.read, //read  0,未读,1,已读
          states: res.data.data.states //states 0-待审核 1-审核成功 2-审核未成功3-提现成功 7 -取消提现
        })
      }
    }, function () {})
    that.getOrderList(0);
    that.onHas();
  },
  //快美协议是否阅读
  onHas() {
    const that = this;
    request._get(getApp().Api_url + 'license/has', {
      userId: userId
    }, function (res) {
      if (res.data.code == 200) {
        console.log('快美协议是否阅读==>', res.data.data)
        if (res.data.data == 0) {
          that.countDown();
          that.setData({
            isHideToast3: true
          })
        }else{
          that.setData({
            isHideToast3: false
          })
        }
      }
    }, function () {})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  countDown: function () {

    let that = this;

    let countDownNum = 10; //获取倒计时初始值

    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    clearInterval(that.data.timer);
    that.setData({

      timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量

        //每隔一秒countDownNum就减一，实现同步

        countDownNum--;

        //然后把countDownNum存进data，好让用户知道时间在倒计着

        that.setData({

          countDownNum: countDownNum

        })

        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来

        if (that.data.countDownNum == 0) {

          //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能

          //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭

          clearInterval(that.data.timer);

          //关闭定时器之后，可作其他处理codes go here

          that.setData({

            countDownNum: 0

          })

        }

      }, 1000)

    })
  },
  onRead() {
    const that = this;
    request._get(getApp().Api_url + 'license/read', {
      userId: userId
    }, function (res) {
      if (res.data.code == 200) {
        console.log('是否同意阅读==>', res.data.data)
        that.setData({
          isHideToast3: false
        })
      }
    }, function () {})
  }

})